import {
  resource
} from '../../../static/resource';
import {
  nav2
} from '../../../tools/nav2';
import {
  ui
} from '../../../tools/ui';
import {
  dateTool
} from '../../../tools/date';
import {
  chatTool
} from './chatTool'

Component({
  properties: {
    target: String,
    sessionOpenId: String,
    userId: String,
    from: String,
    autoMsg: Object,
    notifyMobile: String,
    actions: Object,
    chooseTarget: {
      type: Boolean,
      value: false
    },
    bottom: {
      type: String,
      value: '256rpx'
    },
    left: {
      type: String,
      value: '36rpx'
    },
  },
  data: {
    resource,
    newMsgCount: 0
  },
  lifetimes: {
    ready() {
      this.peekNewMsgCount()
    }
  },
  methods: {
    refresh() {
      this.setData({
        newMsgCount: 0
      })
      this.peekNewMsgCount()
    },
    peekNewMsgCount() {
      wx.cloud.callFunction({
        name: 'chatCmd',
        data: {
          cmd: 'peekNewMsg',
          userOpenId: true
        }
      }).then(res => {
        this.setData({
          newMsgCount: Math.min(res.result.totalNewMsgCount, 99)
        })
      })
    },
    async chatTap(e) {
      // console.log('i-chat chatTap')
      const {
        target, // self | others | list
        sessionOpenId,
        userId,
        from, // orderDetail | goodsDetail
        autoMsg, // { orderId, ticket, shopName } | // send order msg | { goodsId, showTicket, title } // send goods msg
        notifyMobile, // target=='others'
        chooseTarget, // 选择跳转动作
        actions // ui.actionSheet 第一个参数
      } = this.properties

      if (sessionOpenId && userId) await chatTool.setup(sessionOpenId, userId)

      if (chooseTarget) {
        const that = this
        ui.actionSheet(actions, (desc, key) => {
          that.nav2Target(key)
        })
      } else this.nav2Target(target)

      // const target = ui.eventData(e, 'target'), 
      //   sessionOpenId = ui.eventData(e, 'sessionopenid'),
      //   userId = ui.eventData(e, 'userid'),
      //   from = ui.eventData(e, 'from'), 
      //   autoMsg = ui.eventData(e, 'automsg'), 
      //   notifyMobile = ui.eventData(e, 'notifyMobile') 

      this.triggerEvent('tap', e.detail)
    },
    async nav2Target(target) {
      const {
        sessionOpenId,
        userId,
        from,
        autoMsg,
        notifyMobile
      } = this.properties
      // console.log(target, this.properties)
      if (target == 'self') nav2.chat(sessionOpenId, userId, from, autoMsg) // 自己的聊天室
      else if (target == 'list') nav2.chatList() // 聊天列表
      else if (target == 'others') { // admin或master主动与其他用户聊天
        await this.sendSms2User(notifyMobile)
        nav2.chat(sessionOpenId, userId, from, autoMsg)
      }
    },
    async sendSms2User(notifyMobile) {
      if (!notifyMobile) return

      await wx.cloud.callFunction({
        name: 'sendSmsTc',
        data: {
          templateName: "custom_msg",
          templateParamList: [dateTool.format(new Date().getTime(), "M月d日h点"), "1"],
          phoneNumberList: ["+86" + notifyMobile],
          sessionContext: 'notify order user'
        }
      })
    }
  }
})